home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
Collection of Tools & Utilities
/
Collection of Tools and Utilities.iso
/
print
/
gsview10.zip
/
display.c
< prev
next >
Wrap
C/C++ Source or Header
|
1993-08-05
|
18KB
|
696 lines
/*
* display.c -- Ghostscript display operations for GSVIEW.EXE,
* a graphical interface for MS-Windows Ghostscript
* Copyright (C) 1993 Russell Lang
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
*
* Author: Russell Lang
* Internet: rjl@monu1.cc.monash.edu.au
*/
#define STRICT
#include <windows.h>
#include <windowsx.h>
#include <commdlg.h>
#include <shellapi.h>
#include <mmsystem.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <ctype.h>
#include <dir.h>
#include <io.h>
#define NeedFunctionPrototypes 1
#include "ps.h"
#include "gsview.h"
struct ftime dftime; /* time/date of selected file */
long dflength; /* length of selected file */
/* get current media index to papersizes[], or -1 if no match */
int
get_papersizes_index()
{
int i;
for (i=0; papersizes[i].name != (char *)NULL; i++) {
if (!stricmp(papersizes[i].name, medianame))
return i;
}
return -1;
}
/* calculate bitmap size for gswin */
void
gswin_size()
{
int i = get_papersizes_index();
if ( (xdpi == 0.0) || (ydpi == 0.0) )
xdpi = ydpi = DEFAULT_RESOLUTION;
epsf_clipped = FALSE;
switch (orientation) {
case IDM_LANDSCAPE:
case IDM_SEASCAPE:
if (i < 0) {
bitmap_width = user_height;
bitmap_height = user_width;
}
else {
bitmap_width = papersizes[i].height;
bitmap_height = papersizes[i].width;
}
break;
default:
if ((doc != (struct document *)NULL) && doc->epsf
&& epsf_clip) {
epsf_clipped = TRUE;
bitmap_width = doc->boundingbox[URX] - doc->boundingbox[LLX];
bitmap_height = doc->boundingbox[URY] - doc->boundingbox[LLY];
}
else if (i < 0) {
bitmap_width = user_width;
bitmap_height = user_height;
}
else {
bitmap_width = papersizes[i].width;
bitmap_height = papersizes[i].height;
}
}
bitmap_width = (unsigned int)(bitmap_width / 72.0 * xdpi);
bitmap_height = (unsigned int)(bitmap_height / 72.0 * ydpi);
}
/* change the size of the gswin image if open */
void
gswin_resize()
{
BOOL display = FALSE;
BOOL opened_dfile = FALSE;
gswin_size();
if (gswin_hinst == (HINSTANCE)NULL)
return;
if ( (dfile == (FILE *)NULL) && (doc != (struct document *)NULL) ) {
dfreopen();
opened_dfile = TRUE;
}
if (redisplay && page_ready && (doc != (struct document *)NULL))
display = TRUE; /* redisplay page after resize */
gsview_endfile();
if (gswin_hinst != (HINSTANCE)NULL) {
fprintf(cfile,"mark /HWSize [%u %u]\r\n",bitmap_width,bitmap_height);
fprintf(cfile,"/HWResolution [%g %g]\r\n",xdpi,ydpi);
fprintf(cfile,"currentdevice putdeviceprops pop erasepage flushpage\r\n");
pipeflush();
}
if (display) {
if (gswin_hinst != (HINSTANCE)NULL)
gswin_open(); /* we need it open to redisplay */
fix_orientation(cfile);
dsc_header(cfile);
dsc_getpages(cfile,pagenum,pagenum);
pipeflush();
}
if (opened_dfile)
dfclose();
}
void
gsview_orientation(int new_orientation)
{
if (new_orientation == orientation)
return;
if (new_orientation == IDM_SWAPLANDSCAPE) {
swap_landscape = !swap_landscape;
if (swap_landscape)
CheckMenuItem(hmenu, IDM_SWAPLANDSCAPE, MF_BYCOMMAND | MF_CHECKED);
else
CheckMenuItem(hmenu, IDM_SWAPLANDSCAPE, MF_BYCOMMAND | MF_UNCHECKED);
if ((orientation != IDM_LANDSCAPE) && (orientation != IDM_SEASCAPE))
return;
}
else {
CheckMenuItem(hmenu, orientation, MF_BYCOMMAND | MF_UNCHECKED);
orientation = new_orientation;
CheckMenuItem(hmenu, orientation, MF_BYCOMMAND | MF_CHECKED);
}
gswin_resize();
return;
}
void
gsview_media(int new_media)
{
if ( (new_media == media) && (new_media != IDM_USERSIZE) )
return;
CheckMenuItem(hmenu, media, MF_BYCOMMAND | MF_UNCHECKED);
media = new_media;
CheckMenuItem(hmenu, media, MF_BYCOMMAND | MF_CHECKED);
GetMenuString(hmenu, media, medianame, sizeof(medianame), MF_BYCOMMAND);
gswin_resize();
return;
}
/* run Ghostscript for previewing document */
/* return TRUE if ok, FALSE if error */
BOOL
gswin_open()
{
char command[256];
/* return if already open */
if ((gswin_hinst != (HINSTANCE)NULL) && IsWindow(hwndimgchild))
return TRUE;
pipeinit(); /* so we wait for first request */
gswin_size();
sprintf(command,"%s %s -r%gx%g -g%ux%u -sGSVIEW=%u -",
szGSwin, safer ? "-dSAFER" : "", xdpi, ydpi,
bitmap_width, bitmap_height, (unsigned int)hwndimg);
if (strlen(command) > 126) {
info_wait(FALSE);
gserror(IDS_TOOLONG, command, MB_ICONSTOP, SOUND_ERROR);
gswin_hinst = (HINSTANCE)NULL;
return FALSE;
}
gswin_hinst = (HINSTANCE)WinExec(command, SW_SHOWMINNOACTIVE);
if (gswin_hinst < HINSTANCE_ERROR) {
info_wait(FALSE);
gserror(IDS_CANNOTRUN, command, MB_ICONSTOP, SOUND_ERROR);
gswin_hinst = (HINSTANCE)NULL;
return FALSE;
}
if (hwndtext == (HWND)NULL) {
/* we are running an incompatible version of Ghostscript */
hwndtext = FindWindow("BCEasyWin","Ghostscript");
if (hwndtext) {
SendMessage(hwndtext, WM_CHAR, 'q', 1L);
SendMessage(hwndtext, WM_CHAR, 'u', 1L);
SendMessage(hwndtext, WM_CHAR, 'i', 1L);
SendMessage(hwndtext, WM_CHAR, 't', 1L);
SendMessage(hwndtext, WM_CHAR, '\r', 1L);
}
hwndtext = (HWND)NULL;
hwndimgchild = (HWND)NULL;
gswin_hinst = (HINSTANCE)NULL;
clear_timer();
info_wait(FALSE);
gserror(IDS_WRONGGS, NULL, MB_ICONSTOP, SOUND_ERROR);
return FALSE;
}
saved = FALSE;
/* wait for gswin to initialise */
if (set_timer(CLOSE_TIMEOUT))
EnableWindow(hwndimg, FALSE);
while (!is_pipe_done()&& !bTimeout)
do_message(); /* wait for pipe data request from gswin */
clear_timer();
EnableWindow(hwndimg, TRUE);
cfile = pipeopen(); /* open pipe to gswin */
BringWindowToTop(hwndimg);
SetFocus(hwndimg); /* kludge: without this desktop gets focus */
return TRUE;
}
/* close Ghostscript */
BOOL
gswin_close()
{
BOOL force = FALSE;
if (gswin_hinst == (HINSTANCE)NULL)
return TRUE;
if (doc == (struct document*)NULL) {
/* we don't know how many pages remain so we must force an exit */
if (!is_pipe_done())
force = TRUE;
}
else {
if (page_ready)
next_page();
}
if (!force) {
/* try to close Ghostscript cleanly */
pipeclose();
if (set_timer(CLOSE_TIMEOUT))
EnableWindow(hwndimg, FALSE);
while (GetModuleUsage(gswin_hinst) && !bTimeout)
do_message(); /* wait for gswin to close */
clear_timer();
EnableWindow(hwndimg, TRUE);
}
do_message();
/* if still there try killing it a using a brute force method */
if (IsWindow(hwndtext)) {
if (is_win31) {
SendMessage(hwndtext, WM_CLOSE, 0, 0L);
if (IsWindow(hwndtext))
SendMessage(hwndtext, WM_CLOSE, 0, 0L);
}
else {
/* Windows 3.0 hangs if we use SendMessage */
PostMessage(hwndtext, WM_CLOSE, 0, 0L);
do_message();
}
}
do_message();
gswin_hinst = (HINSTANCE)NULL;
hwndimgchild = (HWND)NULL;
hwndtext = (HWND)NULL;
bitmap_scrollx = bitmap_scrolly = 0;
page_ready = FALSE;
saved = FALSE;
pipeclose();
return TRUE;
}
/* send a NEXT_PAGE message to Ghostscript */
void
next_page()
{
int i;
if (hwndimgchild && IsWindow(hwndimgchild)) {
SendMessage(hwndi